package game.test.ia;

import entity.Direction;
import entity.GameEntity;
import entity.Position;
import entity.map.FieldEntity;
import entity.object.EnemyEntity;
import entity.object.PersoEntity;
import game.game.IControllerGame;
import game.game.ia.IGameIAModel;
import game.game.ia.transition.VisionManager;

import java.util.List;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class IAView extends BasicGame
{
	protected Image _niv1;
	protected Image _wall;
	protected Image _perso;
	protected Image _obj;
	protected Image _visio;

	protected IGameIAModel _model;

	protected IControllerGame _controller;

	public final static int POSITION_PLAYER_ON_SCREEN_X = 4;
	public final static int POSITION_PLAYER_ON_SCREEN_Y = 4;

	public IAView(String title, IGameIAModel pModel, IControllerGame pController)
	{
		super( title );
		_model = pModel;
		_controller = pController;
	}

	@Override
	public void render( GameContainer arg0 , Graphics pGraphics ) throws SlickException
	{
		// Affichage du terrain
		for ( int i = 0; i < 10; i++ )
		{
			for ( int j = 0; j < 10; j++ )
			{
				drawRoom( i , j );
			}
		}
		// affichage des autres objets
		if ( _model.getPerso() != null )
		{
			List<GameEntity> listObj = _model.getGameObjects();
			for ( GameEntity obj : listObj )
			{
				if ( obj != _model.getPerso() )
				{
					draw( obj.getPosition().getX() , obj.getPosition().getY() , _obj );
					//
					if ( obj instanceof EnemyEntity )
					{
						EnemyEntity enemy = (EnemyEntity) obj;
						List<Position> positions = new VisionManager().getPositionInVision( _model , enemy , enemy.getVision() );
						for ( Position pos : positions )
						{
							draw( pos.getX() , pos.getY() , _visio );
						}
					}
				}
			}
			//
			//
			pGraphics.drawString( _model.getPerso().getPosition().toString() , 20 , 50 );
		}
		// affichage du joueur
		_perso.draw( POSITION_PLAYER_ON_SCREEN_X * _perso.getWidth() , POSITION_PLAYER_ON_SCREEN_Y * _perso.getHeight() );

	}

	protected void draw( Integer pX , Integer pY , Image pImg )
	{
		int positionX = pX - _model.getPerso().getPosition().getX() + POSITION_PLAYER_ON_SCREEN_X;
		int positionY = pY - _model.getPerso().getPosition().getY() + POSITION_PLAYER_ON_SCREEN_Y;
		if ( positionX >= 0 && positionX < 10 && positionY >= 0 && positionY < 10 )
		{
			pImg.draw( positionX * pImg.getWidth() , positionY * pImg.getHeight() );
		}
	}

	public void drawRoom( int pScreenX , int pScreenY ) throws SlickException
	{
		if ( _model.getPerso() != null )
		{
			int positionX = pScreenX + _model.getPerso().getPosition().getX() - POSITION_PLAYER_ON_SCREEN_X;
			int positionY = pScreenY + _model.getPerso().getPosition().getY() - POSITION_PLAYER_ON_SCREEN_Y;
			FieldEntity fieldEntity = _model.getMap().getFieldEntity( new Position( positionX, positionY ) );

			if ( fieldEntity != null )
			{
				if ( fieldEntity.isBlock() )
				{
					_wall.draw( pScreenX * _wall.getWidth() , pScreenY * _wall.getHeight() );
				}
				else
				{
					_niv1.draw( pScreenX * _niv1.getWidth() , pScreenY * _niv1.getHeight() );
				}
			}
		}
	}

	@Override
	public void init( GameContainer arg0 ) throws SlickException
	{
		_niv1 = new Image( "data/astar3.bmp" );
		_wall = new Image( "data/wall3.bmp" );
		_perso = new Image( "data/perso.bmp" );
		_obj = new Image( "data/obj.bmp" );
		_visio = new Image( "data/visio.bmp" );
	}

	@Override
	public void update( GameContainer gc , int arg1 ) throws SlickException
	{
		if ( _model.getPerso() != null )
		{
			Position persoPos = _model.getPerso().getPosition();
			Input input = gc.getInput();

			if ( input.isKeyPressed( Input.KEY_UP ) )
			{
				changePosition( persoPos.getX() , persoPos.getY() - 1 );
			}
			if ( input.isKeyPressed( Input.KEY_DOWN ) )
			{
				changePosition( persoPos.getX() , persoPos.getY() + 1 );
			}
			if ( input.isKeyPressed( Input.KEY_LEFT ) )
			{
				changePosition( persoPos.getX() - 1 , persoPos.getY() );
			}
			if ( input.isKeyPressed( Input.KEY_RIGHT ) )
			{
				changePosition( persoPos.getX() + 1 , persoPos.getY() );
			}
			if ( input.isKeyPressed( Input.KEY_SPACE ) )
			{
				launchAttack();
			}
		}
	}

	private void changePosition( int pNewX , int pNewY )
	{
		_controller.changePosition( pNewX , pNewY );
	}

	private void launchAttack()
	{
		PersoEntity perso = (PersoEntity) _model.getPerso();
		int x = perso.getPosition().getX();
		int y = perso.getPosition().getX();
		if ( perso.getDirection() == Direction.WEST )
		{
			x--;
		}
		if ( perso.getDirection() == Direction.NORTH )
		{
			y--;
		}
		if ( perso.getDirection() == Direction.SOUTH )
		{
			y++;
		}
		if ( perso.getDirection() == Direction.EAST )
		{
			x++;
		}
		_controller.launchAttack( x , y );
	}

}
